package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
)

type UserController struct {
	beego.Controller
}

type UserObj struct {
	Id       int    `json:"id"`
	Username string `json:"username"`
	Age      int    `json:"age"`
	Address  string `json:"address"`
}

type RespObj struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *UserController) AddUser() {

	user := UserObj{}
	data := c.Ctx.Input.RequestBody
	err := json.Unmarshal(data, &user)

	if err != nil {
		fmt.Println("json 数据解析失败", err.Error())
	}

	fmt.Println("-----------------------------------")
	fmt.Println(user)

	resp := RespObj{
		Code: 200,
		Msg:  "新增用户成功",
		Data: user,
	}
	c.Data["json"] = resp
	c.ServeJSON()
}
